from django.core.management.base import BaseCommand
from apps.role.models import SysUserRole, SysUser, SysRole

class Command(BaseCommand):
    help = 'Import default user-role data'
    def handle(self, *args, **options):
        # 先获取用户和角色
        user1 = SysUser.objects.get(id=1)
        user2 = SysUser.objects.get(id=2)
        user3 = SysUser.objects.get(id=3)
        role_super_admin = SysRole.objects.get(code='SUPER_ADMIN')
        role_admin = SysRole.objects.get(code='ADMIN')
        role_user = SysRole.objects.get(code='USER')

        # 创建用户角色关联对象列表
        user_roles_obj = []
        # 插入用户角色关联，注意先将对象添加到列表中
        user_roles_obj.append(SysUserRole(role=role_super_admin, user=user1))
        user_roles_obj.append(SysUserRole(role=role_admin, user=user2))
        user_roles_obj.append(SysUserRole(role=role_user, user=user3))
        user_roles_obj.append(SysUserRole(role=role_admin, user=user3))

        # 批量插入数据到数据库
        SysUserRole.objects.bulk_create(user_roles_obj)

        # 输出成功信息
        self.stdout.write(self.style.SUCCESS(f'Successfully imported {len(user_roles_obj)} user_roles'))